#! /bin/bash
#
# SDK looking up script, called from Makefile.param
# 

#Search in these directories 
SDK_SEARCH_PATH="/opt ~ /home /root /mnt/hgfs /home/hb/works/hisilicon"
#Only go down for SEARCH_DEPTH subdir
SEARCH_DEPTH=3

HISTORY_FILE=~/.hb_sdk_map

#$1 sdk full path
check_if_is_a_valid_sdk_dir()
{
    if [[ -e $1/sdk.unpack ]]
    then
        return 0
    fi
        return 1
}

append_to_history_file()
{
    sdk_exist=`grep "$1" ${HISTORY_FILE}`
    if [[ ${sdk_exist} == "" ]]
    then
        echo $1 >> ${HISTORY_FILE}
    fi
}

temp_dir=""
#Grep sdk found before in ${HISTORY_FILE}
#$1 sdk dir name like Hi3531_SDK_V1.0.7.1
find_sdk_in_history_file()
{
    if [[ ! -e ${HISTORY_FILE} ]]
    then
        touch ${HISTORY_FILE}
    fi

    temp_dir=`grep "$1" ${HISTORY_FILE} |head -n 1`
    if [[ ${temp_dir} == "" ]]
    then
        return 1
    else
        return 0
    fi
}

#Search sdk in ${SDK_SEARCH_PATH}
#$1 sdk dir name like Hi3531_SDK_V1.0.7.1
find_sdk_by_dir_name()
{
    find_sdk_in_history_file $1
    if [[ "$?" -eq 0 ]]
    then
        append_to_history_file ${temp_dir}
        echo ${temp_dir}
        return 0
    fi

    for dir in ${SDK_SEARCH_PATH}
    do
        temp_dir=`find ${dir} -maxdepth ${SEARCH_DEPTH} -type d -name $1 2>/dev/null`
        check_if_is_a_valid_sdk_dir "${temp_dir}"
        if [[ "$?" -eq 0 ]]
        then
            append_to_history_file ${temp_dir}
            echo ${temp_dir}
            return 0
        fi
    done

    temp_dir=""
    return 1
}


find_sdk_by_dir_name $1

# vim:et:sw=4:ts=4

